var windowName = "login";
var count = 0;
apiready = function(){
    onWindowShow(function () {
        console.log(windowName + " show11111")
        if (count > 0) {
            var phone = getLocData("phone")
            if (!isTrue(phone)) {
                goto("fillPhone")
            }
        }
        count++
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    var Page = createVM("#Page",{
        //页面数据
        form:{}
        ,username:""
        ,password:""
        ,usernameRules: [
            { validate: (val) => !!val, message: '必须填写用户名'},
            { validate: (val) => val.length >= 2, message: '用户名长度大于2'}
        ]
        ,passwordRules: [
            { validate: (val) => !!val, message: '必须填写密码'},
            { validate: (val) => val.length >= 3 && val.length <= 24, message: '密码长度大于3小于25'}
        ]
        ,validateForm: {
            username: '',
            password: ''
        }
        ,msg:"获取验证码"
        ,cur:0
    },{
        //页面方法
        "init":function () {
        },
        sendSms:function () {
            var _this = this;
            var phone = this.username;

            if (!isTrue(phone)) {
                showToast('请填写手机号');
                return false;
            }
            if (!checkIsPhone(phone)) {
                showToast('请检查手机号');
                return false;
            }

            apiGet("/app/send_sms",{phone:phone},function (res,code) {
                logJson(res,"/app/send_sms: ")
                if (code == 200) {
                    var int = 0
                    var start = 60
                    int = setInterval(function () {
                        start--
                        Page.msg = start + "s"
                        if (start == 0) {
                            clearInterval(int)
                            Page.msg = "获取验证码"
                        }
                    },1000)
                }
                showConfirm(res.message,function () {

                },function () {

                })
            });
        }
        ,login:function(){
            var _this = this;

            var phone = this.username;
            var code = this.password;
            var account = this.card;
            var password = this.psw;
                if (!isTrue(phone)) {
                    showToast('请填写手机号')
                    return false;
                }
                if (!isTrue(code)) {
                    showToast('验证码不能为空')
                    return false;
                }
                if (!checkIsPhone(phone)) {
                    showToast('请检查手机号')
                    return false;
                }

                apiGet("/app/login_sms",{phone:phone,code:code},function (res,code) {
                    logJson(res,"/app/login_sms: ")
                    showToast(res.message)
                    if (code == 200) {
                        setLocData("user",res.data)
                        setLocData("login",1)
                        //设置 auth_token
                        setLocData("auth_token",res.data.phone)
                        goback()
                    }
                });

        }
        , tab: function (index) {
            this.cur = index

        }
        ,weChatLogin:function () {
            wxLogin(function (res) {
                logJson(res)

                apiGet("/app/login_wx",{openid:res.openid,unionid:res.unionid,name:res.nickname},function (res,code) {
                    logJson(res,"/app/login_wx: ")
                    showToast(res.message)
                    if (code == 200) {
                        setLocData("user",res.data)
                        setLocData("login",1)
                        //设置 auth_token
                        setLocData("auth_token",res.data.openid)
                        if (res.data.phone == null){
                           showToast("请先完善信息")
                            setTimeout(function () {
                                gointo("fillPhone")
                            },500)
                        }else {
                            goback()
                        }
                    }
                });

            });
        }
        ,kbsubmit:function (event) {
            logJson(event,"event");
            logJson(event.keyCode,"e")
            logJson(this.password,"password")
            if (event.keyCode == 13) {
                this.login();
            }
        }
    })
    Page.init()

}
